package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

/**
 * @author 里
 * @Data:2021/11/05/23:37
 * @Description:
 */
@Configuration
public class CorsConfig {

    // 放入一个bean到 spring 容器中
    @Bean
    public WebFilter webFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 允许所有进行跨域
        corsConfiguration.addAllowedMethod("*"); // 设置允许跨域的方法
        corsConfiguration.addAllowedHeader("*");  // 允许请求头设置数据
        corsConfiguration.setAllowCredentials(true); //  允许携带cookie 数据
        UrlBasedCorsConfigurationSource corsConfigurationSource  = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(corsConfigurationSource);
    }
}
